/*
 * Project Name: jmax
 * File Name: RedisUtils.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.hotel.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

import com.xdtech.common.utils.ApplicationContextUtil;
import com.xdtech.common.utils.JsonUtil;
import com.xdtech.hotel.monitor.ComponentInfo;
import com.xdtech.hotel.monitor.RoomMonitorInfo;
import com.xdtech.hotel.protocol.vo.RCUDevice;

/**
 * 
 * @author <a href="max.zheng@zkteco.com">郑志雄</>
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-4-23 下午2:00:26
 */
public class RedisUtils
{
	private static RedisTemplate<String, String> rt = (RedisTemplate<String, String>) ApplicationContextUtil.getContext().getBean("redisTemplate");
//	private static RedisTemplate<String, String> rt = null;
	private static String RT_EVENT = "RT_EVENT";
	
	/**设备状态*/
	private static String DEVICE_STATUS = "DEVICE_STATUS";
	/**设备访问时间*/
	private static String DEVICE_REQUEST_TIME = "DEVICE_REQUEST_TIME";
	
	/**房间监控信息*/
	private static String ROOM_INFOS = "ROOM_INFOS";
	
	/**设备上传key*/
	private static String DEVICE_POST_DATA = "DEVICE_POST_DATA_";
	
	/**软件上传数据给设备失败key*/
	private static String SOFT_TO_DEVICE_FAIL = "SOFT_TO_DEVICE_FAIL_";
	
	public static void setJsonValue() {
		rt.opsForValue().set("11111", "dfstest");
	}
	
	public static long addDevRtEvent(String content) {
		long rs = rt.opsForList().rightPush(RT_EVENT, content);
		return rs;
	}
	
	public static String getDevRtEvent() {
		return rt.opsForList().leftPop(RT_EVENT);
	}
	
	
//	public static String getDevStatusInfo(String field)
//	{
//		Object obj = rt.opsForHash().get(DEVICE_STATUS, field);
//		return obj == null ? "" : obj.toString();
//	}
	public static RCUDevice getDevStatusInfo(String field)
	{
		Object obj = rt.opsForHash().get(DEVICE_STATUS, field);
		if (null != obj)
		{
			return (RCUDevice) JsonUtil.getDtoFromJsonObjStr(obj.toString(), RCUDevice.class);
		}
		return null;
	}
	
	public static List<RCUDevice> getAllDevStatusInfo()
	{
		List<RCUDevice> rcuDevices = new ArrayList<RCUDevice>();
		Map<Object, Object> map = rt.opsForHash().entries(DEVICE_STATUS);
		RCUDevice rcuDevice = null;
		for (Object sn : map.keySet())
		{
			rcuDevice = (RCUDevice) JsonUtil.getDtoFromJsonObjStr(map.get(sn).toString(), RCUDevice.class);
			rcuDevices.add(rcuDevice);
		}
		return rcuDevices;
	}
	
	
	public static void setDevStatusInfo(RCUDevice rcuDevice)
	{
		
		rt.opsForHash().put(DEVICE_STATUS, rcuDevice.getSn(),JsonUtil.getJsonStr(rcuDevice));
	}
	
	public static void setDevRequestTime(String field, long value)
	{
		rt.opsForHash().put(DEVICE_REQUEST_TIME, field,value+"");
	}
	
	public static long getDevRequestTime(String field)
	{
		Object obj = rt.opsForHash().get(DEVICE_REQUEST_TIME, field);
		return (Long) (obj == null ? -1 : obj);
	}
	
	public static Map<String, Long> getAllDeviceRequests()
	{
		Map<Object, Object> map = rt.opsForHash().entries(DEVICE_REQUEST_TIME);
		Map<String, Long> rsMap = new HashMap<String, Long>();
		for (Object objkey : map.keySet())
		{
			rsMap.put(objkey.toString(), Long.valueOf(map.get(objkey).toString()));
		}
		return rsMap;
	}
	
	public static void main(String[] args)
	{		
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-redis.xml");
		rt = (RedisTemplate<String, String>) ac.getBean("redisTemplate");
//		rt.opsForList().leftPush("max", "test2");
//		System.out.println(rt.opsForList().range("max", 0, -1));
//		System.out.println(rt.opsForList().size("max"));
//		setDevStatusInfo("123456789", "121121321");
//		setDevStatusInfo("9852147", "121121321");
//		System.out.println(getDevStatusInfo("123456789"));
//		System.out.println(getDevStatusInfo("9852147"));
		getAllRoomInfos();
	}

	/**
	 * 加载房间监控数据
	 * @author max.zheng
	 * @create 2015-5-1下午4:50:35
	 * @modified by
	 * @return
	 */
	public static Collection<? extends RoomMonitorInfo> getAllRoomInfos() {
		List<RoomMonitorInfo> roomMonitorInfos = new ArrayList<RoomMonitorInfo>();
		Map<Object, Object> map = rt.opsForHash().entries(ROOM_INFOS);
		RoomMonitorInfo roomMonitorInfo = null;
		for (Object sn : map.keySet())
		{
			Map<String, Class> classMap = new HashMap<String, Class>();
			classMap.put("id", Long.class);
			classMap.put("sn", String.class);
			classMap.put("status", String.class);
			classMap.put("roomNo", String.class);
			classMap.put("componentInfos", ComponentInfo.class);
			roomMonitorInfo = (RoomMonitorInfo) JsonUtil.getObjByJson(map.get(sn).toString(), RoomMonitorInfo.class,classMap);
//			List<ComponentInfo> componentInfos = roomMonitorInfo.getComponentInfos();
//			for (ComponentInfo componentInfo : componentInfos) {
//				System.out.println(componentInfo.getStatus());
//			}
			roomMonitorInfos.add(roomMonitorInfo);
		}
		return roomMonitorInfos;
	}
	/**
	 * 设置房间监控数据 key 房间号
	 * 
	 * @author max.zheng
	 * @create 2015-5-1下午4:57:22
	 * @modified by
	 * @param roomMonitorInfo
	 */
	public static void setRoomInfo(RoomMonitorInfo roomMonitorInfo)
	{
		rt.opsForHash().put(ROOM_INFOS, roomMonitorInfo.getRoomNo(),JsonUtil.getJsonStr(roomMonitorInfo));
	}

	/**
	 * 移除redis里房间信息
	 * @author max.zheng
	 * @create 2015-5-1下午7:01:28
	 * @modified by
	 * @param roomNo
	 */
	public static void removeRoomInfo(String roomNo) {
		rt.opsForHash().delete(ROOM_INFOS, roomNo);
	}

	/**
	 * 根据不同sn号，存放不同设备待处理的上传数据到redis
	 * @author max.zheng
	 * @create 2015-5-4下午8:20:49
	 * @modified by
	 * @param sn
	 * @param postContent
	 */
	public static void addDevicePostContent(String sn, String postContent) {
		rt.opsForList().rightPush(DEVICE_POST_DATA+sn,postContent);
	}
	
	public static Set<String> checkExistPostContent() {
		Set<String> keys = rt.keys(DEVICE_POST_DATA+"*");
//		return keys!=null&&keys.size()>0?true:false;
		return keys;
	}
	
	public static String getDevicePostDataByKey(String key) {
		String postContent = rt.opsForList().leftPop(key);
		return postContent;
	}
	
	
//	public static void addFailPostToDevice(String sn,String cmd) {
//		rt.opsForList().
//	}
	/**Soft请求给设备key*/
	private static String POST_DATA_TO_DEVICE = "POST_DATA_TO_DEVICE_";
	public static void addPostCmdToDevcie(String sn,String cmd) {
		rt.opsForList().rightPush(POST_DATA_TO_DEVICE+sn, cmd);
	}
	/**获取设备请求返回值*/
	public static String getPostCmdToDevcie(String key) {
		return rt.opsForList().leftPop(key);
	}
	/**根据key获取SN*/
	public static String getSNByKey(String key) {
		return key.replaceAll(POST_DATA_TO_DEVICE, "");
	}
	/**检查是否存在请求给设备命令和对应返回值*/
	public static Set<String> checkExistPostToDevice() {
		Set<String> keys = rt.keys(POST_DATA_TO_DEVICE+"*");
		return keys;
	}
	
	/**Soft第二次请求给设备key*/
	private static String POST_SECOND_DATA_TO_DEVICE = "POST_SECOND_DATA_TO_DEVICE_";
	public static void addNeedSecondPostCmdToDevcie(String sn,String cmd) {
		rt.opsForList().rightPush(POST_SECOND_DATA_TO_DEVICE+sn, cmd);
	}
	/**获取设备请求返回值*/
	public static String getSecondPostCmdToDevcie(String key) {
		return rt.opsForList().leftPop(key);
	}
	/**根据第二次key获取SN*/
	public static String getSNBySecondKey(String key) {
		return key.replaceAll(POST_SECOND_DATA_TO_DEVICE, "");
	}
	/**检查是否存在需第二次请求给设备命令和对应返回值*/
	public static Set<String> checkExistSecondPostToDevice() {
		Set<String> keys = rt.keys(POST_SECOND_DATA_TO_DEVICE+"*");
		return keys;
	}
	
}
